package com.beiye.drivertransport.SubActivity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.adapter.LearnPagerAdapter;
import com.beiye.drivertransport.specifiedLearning.business.CourseLearnBusinessActivity;
import com.beiye.drivertransport.specifiedLearning.business.CourseSupermarketBusinessFragment;
import com.beiye.drivertransport.specifiedLearning.business.SpecifiedLearningBusinessFragment;
import com.beiye.drivertransport.utils.Utils;
import com.githang.statusbar.StatusBarCompat;

import java.lang.reflect.Field;
import java.util.ArrayList;

//企业管理员学习计划和课程超市
public class SubBusinessManagemenSafeLearnActivity extends TwoBaseAty {


    TabLayout TabLayout1;

    TextView supermarket;

    ImageView img_businesssafeback;
    private  int TAB_MARGIN_DIP = 25;

    ViewPager myviewpager;
    private ArrayList<Fragment> fragments;
    private LearnPagerAdapter adapter;
    private ArrayList<String> list_title;
    private int readInterval;
    private int mark;
    private String orgId;
    @Override
    public int getLayoutId() {
        return R.layout.activity_sub_business_managemen_safe_learn;
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.supermarket) {
            if (Utils.isFastClicker()) {
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putInt("readInterval", readInterval);
            bundle.putInt("Mark", mark);
            bundle.putString("orgId", orgId);
            startActivity(CourseLearnBusinessActivity.class, bundle);
        } else if (id == R.id.img_businesssafeback) {
            finish();
        }
    }
    @Override
    public void initData() {
        TabLayout1 = (TabLayout) findViewById(R.id.tab_FindFragment_title1);
        supermarket = (TextView) findViewById(R.id.supermarket);
        img_businesssafeback = (ImageView) findViewById(R.id.img_businesssafeback);
        myviewpager = (ViewPager) findViewById(R.id.myviewpager);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));

        Bundle bundle = getIntent().getExtras();
        readInterval = bundle.getInt("readInterval");
        mark = bundle.getInt("Mark");
        orgId = bundle.getString("orgId");
        int minPer = bundle.getInt("minPer");
        int ftId = bundle.getInt("ftId");
        String adId = bundle.getString("adId");
        int photoMark = bundle.getInt("photoMark");
        int faceRecgMark=bundle.getInt("faceRecgMark");

        fragments = new ArrayList<Fragment>();
        CourseSupermarketBusinessFragment courseSupermarketBusinessFragment = new CourseSupermarketBusinessFragment();
        fragments.add(courseSupermarketBusinessFragment);
        SpecifiedLearningBusinessFragment specifiedLearningBusinessFragment = new SpecifiedLearningBusinessFragment();
        fragments.add(specifiedLearningBusinessFragment);

        Bundle args1 = new Bundle();
        args1.putString("orgId", orgId);
        args1.putInt("photoMark",photoMark);
        args1.putInt("minPer",minPer);
        args1.putInt("faceRecgMark",faceRecgMark);
        courseSupermarketBusinessFragment.setArguments(args1);
        Bundle args2 = new Bundle();
        args2.putString("orgId", orgId);
        args2.putString("adId", adId);
        args2.putInt("ftId",ftId);
        args2.putInt("minPer",minPer);
        args2.putInt("faceRecgMark",faceRecgMark);
        specifiedLearningBusinessFragment.setArguments(args2);
        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        list_title = new ArrayList<>();
        list_title.add("企业发布");
        list_title.add("行管发布");
        TabLayout1.setTabMode(TabLayout.MODE_FIXED);
        TabLayout1.addTab(TabLayout1.newTab().setText(list_title.get(0)));
        TabLayout1.addTab(TabLayout1.newTab().setText(list_title.get(1)));
        adapter = new LearnPagerAdapter(getSupportFragmentManager(),fragments,list_title);
        myviewpager.setAdapter(adapter);
        TabLayout1.setupWithViewPager(myviewpager);
        setIndicator(SubBusinessManagemenSafeLearnActivity.this, TabLayout1, TAB_MARGIN_DIP, TAB_MARGIN_DIP);
    }
    //修改tablayout的tabIndicator宽度
    public  void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field slidingTabIndicator = null;
        try {
            slidingTabIndicator = tabLayout.getDeclaredField("slidingTabIndicator");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        slidingTabIndicator.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) slidingTabIndicator.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (getDisplayMetrics(context).density * leftDip);
        int right = (int) (getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
    public DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric;
    }

    public  float getPXfromDP(float value, Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                context.getResources().getDisplayMetrics());
    }
    @Override
    public void requestData() {

    }
}
